const { publishProject, updateProject, getProjectList, getProjectByTitle, getProjectById } = require('../dao/projects')

//发布项目
function ProjectPublish(params) {
  return new Promise(async (resolve, reject) => {
    const { admin_id, title, info, people_num, categorization, start_time, over_time } = params
    if (!admin_id || !title || !info || !people_num || !categorization || !start_time || !over_time) {
      resolve(error('参数不全'))
    }
    publishProject(params).then(() => {
      resolve(ok())
    }).catch(err => {
      resolve(error(err))
    })
  })
}

//修改项目信息
function ProjectUpdate(params) {
  return new Promise(async (resolve, reject) => {
    const { project_id, title, info, people_num, categorization, start_time, over_time,status } = params
    if (!project_id || !title || !info || !people_num || !categorization || !start_time ||!over_time||!status) {
      resolve(error('参数不全'))
    }
    updateProject(params).then(() => {
      resolve(ok())
    }).catch(err => {
      resolve(error(err))
    })
  })
}

//根据admin获取项目列表
function ProjectList(params) {
  return new Promise(async (resolve, reject) => {
    const { admin_id } = params
    if (!admin_id) {
      resolve(error('参数不全'))
    }
    getProjectList(admin_id).then((data) => {
      resolve(ok({ data }))
    }).catch(err => {
      resolve(error(err))
    })
  })
}

// title模糊查询
function ProjectListByTitle(params) {
  return new Promise(async (resolve, reject) => {
    getProjectByTitle(params).then((data) => {
      resolve(ok({ data }))
    }).catch(err => {
      resolve(error(err))
    })
  })
}

//根据id获取详情
function ProjectDetail(params) {
  return new Promise(async (resolve, reject) => {
    getProjectById(params).then((data) => {
      resolve(ok({ data }))
    }).catch(err => {
      resolve(error(err))
    })
  })
}


module.exports = {
  ProjectPublish, ProjectUpdate, ProjectList, ProjectListByTitle, ProjectDetail
}